/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.oplink.webems.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.common.utils.CacheUtils;
import com.oplink.webems.modules.sys.entity.Dict;
import com.oplink.webems.modules.sys.entity.User;
import com.oplink.webems.modules.sys.service.DictService;
import com.oplink.webems.modules.sys.service.ShiroDbRealm.ShiroUser;

/**
 * 字典Controller
 * 
 * @author ThinkGem
 * @version 2013-3-23
 */
@Controller
@RequestMapping(value = "sys/dict")
public class DictController extends BaseController {

	@Autowired
	private DictService dictService;

	@ModelAttribute
	public Dict get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return dictService.getDict(id);
		} else {
			return new Dict();
		}
	}

	@RequestMapping(value = { "list", "" })
	public String list(Dict dict, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Dict> page = dictService.find(new Page<Dict>(request, response), dict);
		model.addAttribute("page", page);
		model.addAttribute("dict", dict);
		return "modules/sys/dictList";
	}

	@RequestMapping(value = "rstCache", method = RequestMethod.GET)
	@ResponseBody
	public String rstCache() {
		try {
			CacheUtils.clearAll();
			return "0";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "1";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Dict updateForm(@PathVariable("id") Long id) {
		Dict dict = dictService.getDict(id);
		dict.setUser(null);
		return dict;
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute("dict") Dict dict, RedirectAttributes redirectAttributes) {
		try {
			User user = new User(getCurrentUserId());
			dict.setUser(user);
			dictService.saveDict(dict);
			redirectAttributes.addFlashAttribute("message", "更新任务成功");
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}

	@RequestMapping(value = "form")
	public String form(Dict dict, Model model) {
		model.addAttribute("dict", dict);
		return "modules/sys/dictForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("userId", getCurrentUserId());
		model.addAttribute("action", "create");
		return "modules/sys/dict";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(@Valid Dict dict, RedirectAttributes redirectAttributes, Model model) {
		try {
			User user = new User(getCurrentUserId());
			dict.setUser(user);
			model.addAttribute("dict", dict);
			dictService.saveDict(dict);
			redirectAttributes.addFlashAttribute("message", "创建任务成功");
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public String delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
		try {
			String ret = "0";
			if (!"".equals(id)) {
				String[] delList = id.split(",");
				for (String idItem : delList) {
					dictService.deleteDict(Long.valueOf(idItem));
				}
				redirectAttributes.addFlashAttribute("message", "删除任务成功");
				ret = "1";
			} else {
				ret = "0";
			}
			return ret;
		} catch (Exception e) {
			return "0";
		}
	}

	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}
}
